package cn.rslee.demo.spring.mybatis.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;

import cn.rslee.demo.spring.mybatis.dao.AccessLogDao;
import cn.rslee.demo.spring.mybatis.entity.AccessLog;
import cn.rslee.demo.spring.mybatis.entity.AccessLogCount;
import cn.rslee.demo.spring.mybatis.entity.DateParam;
import cn.rslee.server.IPage;
import cn.rslee.server.SimplePage;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2016年2月26日      rslee         Create
 * ****************************************************************************
 * </pre>
 * @author rslee
 * @since 1.0
 */
@Component("xmlAccessLogService")
public class XmlAccessLogServiceImpl implements XmlAccessLogService {
	
	@Resource
	private SqlSession sqlSession;
	
	/**
	 * 
	 * spring自动代理实现类
	 */
	@Resource
	private AccessLogDao accessLogDao;
	
	/**
	 * 
	 * 具体的自己写的实现类
	 */
	@Resource
	private AccessLogDao accessLogDaoImpl;
	
	@Override
	public AccessLog createByName(AccessLog log) {
		AccessLogDao dao = sqlSession.getMapper(AccessLogDao.class);
		dao.save(log);
		return log;
	}

	/* 
	 * 这里是演示，没有自己实现类的话，mybatis可以帮你代理一个出来
	 * @see cn.rslee.demo.spring.mybatis.service.XmlAccessLogService#create(cn.rslee.demo.spring.mybatis.entity.AccessLog)
	 */
	@Override
	public AccessLog create(AccessLog log) {
		accessLogDao.save(log);
		return log;
	}

	@Override
	public AccessLog update(long id, AccessLog log) {
		AccessLog oldLog = accessLogDao.findOne(id);
		if(oldLog != null){
			oldLog.setSysCode(log.getSysCode());
			oldLog.setUsername(log.getUsername());
			accessLogDao.update(oldLog);
		}
		return oldLog;
	}

	@Override
	public AccessLog patchUpdate(long id, AccessLog log) {
		AccessLog oldLog = accessLogDao.findOne(id);
		if(oldLog != null){
			oldLog.setSysCode(log.getSysCode());
			oldLog.setUsername(log.getUsername());
			accessLogDao.update(oldLog);
		}
		return oldLog;
	}

	@Override
	public List<AccessLog> findAll() {
		return accessLogDao.findAll();
	}

	@Override
	public AccessLog get(long id) {
		return accessLogDao.findOne(id);
	}

	@Override
	public IPage<AccessLog> findByPage(int page, int size) {
		long total = accessLogDao.findCountBy();
		List<AccessLog> datas = accessLogDao.findByLimit(SimplePage.calculateFrom(page, size), size);
		
		return new SimplePage<AccessLog>(datas,total,page,size);
	}

	@Override
	public AccessLog delete(long id) {
		AccessLog log = accessLogDao.findOne(id);
		if(log != null){
			accessLogDao.delete(id);
		}
		return log;
	}

	@Override
	public List<AccessLog> findByUsernameAndSysCode(String username,
			String sysCode) {
		return accessLogDao.findBy(username, sysCode);
	}

	@Override
	public List<AccessLog> findByUsernameAndOrder(String username,
			String orderBy) {
		
		String order = ORDERS.get(orderBy);
		order = order == null ? "id" : order;
		return accessLogDao.findByOrder(username, order);
	}
	
	private final static Map<String,String> ORDERS = new HashMap<String,String>();
	
	static{
		ORDERS.put("id", "id");
		ORDERS.put("sysCode", "sys_code");
		ORDERS.put("createTm", "create_tm");
	}

	@Override
	public List<AccessLogCount> groupBy(String groupBy) {
		return accessLogDao.groupBy(groupBy);
	}

	@Override
	public List<AccessLog> findByParams(String sysCode, String username,
			DateParam visitTm) {
		
		return accessLogDao.findByParams(sysCode, username, visitTm.getDate());
	}

	@Override
	public List<AccessLog> findByQueryParams(String sysCode, String username,
			DateParam visitTm) {
		Map<String,Object> queryParams = new HashMap<String,Object>();
		
		queryParams.put("sysCode", sysCode);
		queryParams.put("username", username);
		queryParams.put("visitTm", visitTm.getDate());
		
		return accessLogDao.findByQueryParams(queryParams);
	}

	
}
